<style type="text/css">
    .action-cell {
        margin-bottom: 20px;
        width: 100%;
        display: -webkit-box;
        display: -ms-flexbox;
        display: flex;
        -ms-flex-pack: distribute;
        justify-content: space-between;
        align-items: center;
        margin-right: 20%;
    }

    .search-block {
        display: flex;
        margin-bottom: 10px;
    }

    .input-search {
        margin-right: 10px;
    }
</style>
<template>
    <div>
        <div class="action-cell">
            <div class="list-action-btn">
                <Button type="error" size="large" @click="addIsShow =true">添加</Button>
            </div>
        </div>
        <Table :context="self" :data="tableData" :columns="tableColumns" stripe border></Table>
        <div style="margin: 10px;overflow: hidden">
            <div style="float: right;">
                <Page :total="total" size="large" show-total :current="current" show-elevator show-sizer
                      @on-change="changePage"></Page>
            </div>
        </div>
        <!--添加-->
        <Modal v-model="addIsShow" title="添加" @on-ok="handleSubmit('formValidate')"
               @on-cancel="setPwdIsShow =false">
            <Form ref="formValidate" :model="formValidate" :rules="ruleValidate" :label-width="100">
                <Form-item label="卡号" prop="Cardid">
                    <i-input v-model="formValidate.Cardid" placeholder="请输入卡号"></i-input>
                </Form-item>
                <Form-item label="类型" prop="title">
                    <i-select v-model="formValidate.Cardtypeid" placeholder="请选择类型" style="width:200px">
                        <template v-for="(item,index) in cardType">
                            <i-option :value="item.Id">{{item.Title}}</i-option>
                        </template>
                    </i-select>
                </Form-item>
            </Form>
        </Modal>
        <Modal v-model="addIsShow2" title="查看" @on-ok="handleSubmitEdit('formValidate2')"
               @on-cancel="addIsShow2 =false">
            <Form ref="formValidate2" :model="formValidate2" :rules="ruleValidate2" :label-width="100">
                <Form-item label="编号前缀" prop="prefix">
                    <i-input v-model="formValidate2.prefix" disabled placeholder="请输入编号前缀"></i-input>
                </Form-item>
            </Form>
        </Modal>
    </div>
</template>
<script>
    import apiUtil from '../../assets/utils/api_util';

    export default {
        data() {
            return {
                self: this,
                tableData: [],
                total: 0,
                current: 1,
                //添加
                formValidate: {
                    //添加字段
                    Cardid: '',
                    Cardtypeid: '',
                },
                addIsShow: false,
                cardType:[],
                ruleValidate: {},
                //编辑
                formValidate2: {
                    //添加字段
                    title: '',
                },
                addIsShow2: false,
                ruleValidate2: {},
                tableColumns: [
                    {
                        title: '一卡通号',
                        key: 'Cardid',
                        align: 'center',
                    },
                    {
                        title: '类型',
                        key: 'CardType',
                        align: 'center',
                    },
                    {
                        title: '操作',
                        key: 'action',
                        align: 'center',
                        render: (h, params) => {

                            return h('div', [
                                    h('Button', {
                                        props: {
                                            type: 'error',
                                            size: 'small'
                                        },
                                        style: {
                                            'margin-right': '10px',
                                        },
                                        on: {
                                            click: () => {
                                                this.remove(params.row.Cardid)
                                            }
                                        }
                                    }, '删除'),
                                    h('Button', {
                                        props: {
                                            type: 'error',
                                            size: 'small'
                                        },
                                        style: {
                                            'margin-right': '10px',
                                        },
                                        on: {
                                            click: () => {
                                                this.resetClick(params.row.Cardid)
                                            }
                                        }
                                    }, '重置'),
                                    // h('Button', {
                                    //     props: {
                                    //         type: 'error',
                                    //         size: 'small'
                                    //     },
                                    //     style: {
                                    //         'margin-right': '10px',
                                    //     },
                                    //     on: {
                                    //         click: () => {
                                    //             this.editClick(params.row)
                                    //         }
                                    //     }
                                    // }, '查看'),
                                ]
                            )

                        }
                    }
                ]
            }
        },
        mounted() {
            this.mockTableData();
            this.getCardType();
        },
        components: {},
        methods: {
            handleEdit(row) {
                this.$set(row, '$isEdit', true)
            },
            handleSave(row) {
                this.$set(row, '$isEdit', false);

                this.tableEditSave(row);
                console.log('值:', row);

            },
            handleSubmit(name) {
                let that = this;
                let model = this.$refs[name].model;
                this.$refs[name].validate((valid) => {
                    that.comfirmModifyPS(model);
                })
            },
            comfirmModifyPS: function (model) {
                let that = this;
                let params = that.$qs.stringify(model);
                apiUtil.post(that, apiUtil.urls.OneCardAdd, params, function (res) {
                    that.mockTableData();
                });
            },
            //获取一卡通类型列表
            getCardType: function () {
                let that = this;
                let params = {};
                apiUtil.get(that, apiUtil.urls.GetCardTypes, params, function (res) {
                    that.cardType =res.Msg;
                });
            },
            //点击编辑
            editClick: function (row) {

                this.addIsShow2 = true;
                this.formValidate2.title = row.Keyname;
                this.formValidate2.id = row.Id;

                console.log("formValidate2:", this.formValidate2);
                console.log("row:", row)
                // this.formValidate2.Text =row.Intro;
            },
            handleSubmitEdit(name) {
                let that = this;
                let model = this.$refs[name].model;
                this.$refs[name].validate((valid) => {
                    that.comfirmModifyPSEdit(model);
                })
            },
            comfirmModifyPSEdit: function (model) {
                let that = this;
                let params = {};
                console.log('model:', model);
                params = that.$qs.stringify(model);
                console.log('params:', params);
                apiUtil.post(that, apiUtil.urls.EditDuty, params, function (res) {
                    that.$Message.info('修改成功 ');
                    that.mockTableData();
                });
            },
            //跳转
            jumpPage: function (id, url) {
                //跳转
                let route = '';
                if (id > 0) {
                    route = {
                        path: url,
                        query: {
                            id: id,
                            value: ''
                        },
                    };
                } else {
                    route = {
                        path: url,
                    };
                }

                this.$router.push(route);
            },
            //列表总数据
            mockTableData() {
                let that = this;

                let para = {
                    currPage: this.current,
                    pageSize: 10,
                };
                let params = that.$qs.stringify(para);

                this.$Loading.start();

                apiUtil.get(that, apiUtil.urls.GetCardsList, para, function (res) {
                    that.$Loading.finish();
                    // that.tableData = res.Msg;

                    that.total = res.Msg.TotalCount;
                    that.current = res.Msg.CurrentPage;
                    that.tableData = res.Msg.Arrays;
                    console.log('res:', res)
                });

            },
            changePage(row) {
                // 这里直接更改了模拟的数据，真实使用场景应该从服务端获取数据
                console.log("row", row);
                this.current = row;
                this.mockTableData();
            },
            //删除
            remove(index) {
                let self = this;
                let that = this;
                this.$Modal.confirm({
                    title: '提示',
                    content: `是否删除此记录`,
                    onOk: function () {
                        this.$Loading.start();
                        let para = {cardId: index};
                        let params = that.$qs.stringify(para);
                        apiUtil.post(that, apiUtil.urls.DelCard, params, function (res) {
                            that.mockTableData();
                        });
                    }
                })
            },
            //重置
            resetClick(index) {
                let self = this;
                let that = this;
                this.$Modal.confirm({
                    title: '提示',
                    content: `确定重置一卡通吗`,
                    onOk: function () {
                        this.$Loading.start();
                        let para = {cardId: index};
                        let params = that.$qs.stringify(para);
                        apiUtil.post(that, apiUtil.urls.ResetCard, params, function (res) {
                            that.mockTableData();
                            that.$Notice.open({
                                title: '通知',
                                desc: '重置成功 '
                            });
                        });
                    }
                })
            },
        },
    }
</script>
